package com.help10000.rms.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amir.common.beans.OrderItem
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseSingleItemTypeRecyclerViewAdapter
import com.amir.common.utils.IMEUtil
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.OrderPresenter
import com.help10000.rms.mvp.view.ICustomKeyValueItemView
import com.help10000.rms.ui.activitys.*
import com.lxj.xpopup.XPopup

class OrderAdapter(
    context: Context?,
    dataList: List<OrderItem>,
    presenter: OrderPresenter,
    customItemView: ICustomKeyValueItemView,
    resId: Int,
    orderType: Int = 0,
) :
    BaseSingleItemTypeRecyclerViewAdapter<OrderItem>(context, dataList, resId) {
    private var mPresenter: OrderPresenter = presenter
    private var orderType: Int = orderType
    private var customItemView = customItemView
    override fun bindData(holder: CommonViewHolder, order: OrderItem, position: Int) {

        holder.setText(R.id.tv_date, text = "${order.RepairInfo.Title}")
        holder.setText(R.id.tv_num, text = "${order.RepairInfo.Seq}")
        holder.setText(R.id.tv_price, text = "${order.RepairInfo.Right}")

        var recyclerView = holder.getView<RecyclerView>(R.id.itemsRecyclerView)
        recyclerView.apply {
            itemAnimator = null
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = OrderChildItemAdapter(context, order.RepairInfo.Items, customItemView)
        }
        var buttons = order.Buttons
        holder.setViewVisibility(
            R.id.tv_lingliao,
            if (buttons.ShowJxc) View.VISIBLE else View.GONE
        )
        holder.setViewVisibility(
            R.id.tv_refuse,
            if (buttons.ShowAssReject) View.VISIBLE else View.GONE
        )
        holder.setViewVisibility(
            R.id.tv_gathering,
            if (buttons.ShowPay) View.VISIBLE else View.GONE
        )
        holder.setViewVisibility(
            R.id.tv_roborder,
            if (buttons.ShowQiangDan) View.VISIBLE else View.GONE
        )
        holder.setViewVisibility(
            R.id.tv_confirmOrder,
            if (buttons.ShowAssConfirm) View.VISIBLE else View.GONE
        )
        holder.setViewVisibility(
            R.id.tv_report,
            if (buttons.ShowRepairDone) View.VISIBLE else View.GONE
        )
        if (buttons.ShowRepairDone) {
            holder.setViewVisibility(R.id.tv_report, View.VISIBLE)
            holder.setText(R.id.tv_report, buttons.RepairDoneText)
        } else {
            holder.setViewVisibility(R.id.tv_report, View.GONE)
        }
        holder.setOnClickListener(R.id.tv_refuse) {
            val intent = Intent(mContext, RefuseOrderActivity::class.java)
            intent.putExtra("data", order.RepairInfo)
            mContext.startActivity(intent)
        }
        holder.setOnClickListener(R.id.tv_report) {
            val intent = Intent(mContext, ReportOrderActivity::class.java)
            intent.putExtra("data", order.RepairInfo)
            (mContext as Activity).startActivityForResult(intent, 1)
        }
        holder.setOnClickListener(R.id.tv_gathering) {
            val intent = Intent(mContext, CashEntryActivity::class.java)
            intent.putExtra("data", order.RepairInfo)
            mContext.startActivity(intent)
        }
        holder.setOnClickListener(R.id.tv_lingliao) {
            val intent = Intent(mContext, PickingMaterialActivity::class.java)
            intent.putExtra("data", order.RepairInfo)
            mContext.startActivity(intent)
        }
        holder.setOnClickListener(R.id.tv_confirmOrder) {
            XPopup.Builder(mContext).asConfirm("", "确定接单吗？") {
                //接单
                mPresenter.confirmOrder(order.RepairInfo.ID!!, mContext as OrderActivity)
            }.show()
        }
        holder.setOnClickListener(R.id.tv_roborder) {
            XPopup.Builder(mContext).asConfirm("", "确定抢单吗？") {
                //抢单
                mPresenter.robOrder(order.RepairInfo.ID!!, mContext as OrderActivity)
            }.show()
        }
        //仅我的订单，超时订单可进入详情页
        if (orderType != 1) {
            holder.setOnClickListener(R.id.orderTitle) {
                val intent = Intent(mContext, OrderDetailActivity::class.java)
                intent.putExtra("repairId", order.RepairInfo.ID)
                mContext.startActivity(intent)
            }
            holder.setCommonClickListener(object : CommonViewHolder.onItemCommonClickListener {
                override fun onItemClickListener(position: Int) {
                    IMEUtil.hideIME(mContext, holder.itemView)
                }

                override fun onItemLongClickListener(position: Int) {

                }
            })
        }
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

}
